<?php

namespace Biz\model\brandAccount;

use Biz\model\wiki\Wiki;
use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\tools\ImgUrl;

class BrandAccountShop extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'brand_account_shop';

    const SHOP_STATUS_VALID = 1;
    const SHOP_STATUS_NOVALID = 0;
    const SHOP_STATUS = [
        self::SHOP_STATUS_VALID => '有效',
        self::SHOP_STATUS_NOVALID => '失效',
    ];

    const SHOP_TYPE = [
        Wiki::CHANNEL_TYPE_TAOBAO => '淘宝',
        Wiki::CHANNEL_TYPE_TMALL => '天猫',
        Wiki::CHANNEL_TYPE_JD => '京东',
        4 => '其他',
    ];

    const SHOP_TYPE_WIKI = [
        Wiki::CHANNEL_TYPE_TAOBAO,
        Wiki::CHANNEL_TYPE_TMALL,
        Wiki::CHANNEL_TYPE_JD
    ];


    public static function getInfoByShopId($shopId, $type)
    {
        return self::getOneByWhere(['shop_id' => $shopId, 'type' => $type, 'shop_status' => self::SHOP_STATUS_VALID]);
    }

    public static function getAllByWhere($where = [], $files = '*')
    {
        $data = self::select($files, $where);
        if (!$data) {
            return [];
        }
        return array_map(function ($_data) {
            return self::_filter($_data);
        }, $data);
    }

    public static function _filter($data)
    {
        if ($data) {
            foreach ($data as $_k => &$_v) {
                switch ($_k) {
                    case 'letter':
                        $data['letter_img'] = !empty($_v) ? ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $_v, ImgUrl::IMAGE_SIZE_ORI_PHONE_BIG) : '';
                        break;
                    case 'authorization':
                        $data['authorization_img'] = !empty($_v) ? ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $_v, ImgUrl::IMAGE_SIZE_ORI_PHONE_BIG) : '';
                        break;
                }
            }
        }
        return $data;
    }

    public static function getOneByWhere($where = [], $files = '*')
    {
        $data = self::get($files, $where);
        return $data;
    }

    public static function insertData($data)
    {
        if (!isset($data['add_time'])) {
            $data['add_time'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edit_time'])) {
            $data['edit_time'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }

    public static function updateData($data, $where)
    {
        if (!isset($data['edit_time'])) {
            $data['edit_time'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }
        return true;
    }

}